package com.zoroki.hmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zoroki.hmusic.entity.Comments;
import com.zoroki.hmusic.mapper.CommentsMapper;
import com.zoroki.hmusic.service.ICommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zoroki
 * @since 2023-06-11
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements ICommentsService {

    @Autowired
    private CommentsMapper commentsMapper;
    @Override
    public List<Comments> findCommentListFromCommentsBySongId(int songId) {
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("song_id", songId);
        return commentsMapper.selectList(wrapper);
    }

    @Override
    public List<Comments> findReplyListFromCommentsByReplyId(int replyId) {
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("reply_id", replyId);
        return commentsMapper.selectList(wrapper);
    }

    @Override
    public Comments addComments(Comments comments) {
        int insert = commentsMapper.insert(comments);
        if (insert > 0) {
            return comments;
        }
        return null;
    }

    @Override
    public int deleteCommentById(int id) {
        //1.查找所有
        List<Comments> commentsByReplyId = findReplyListFromCommentsByReplyId(id);

        QueryWrapper<Comments> wrapper = new QueryWrapper<>();

        for (Comments comments : commentsByReplyId) {
            deleteCommentById(comments.getId());
            wrapper.eq("reply_id", comments.getId());
            commentsMapper.delete(wrapper);
        }

        return commentsMapper.deleteById(id);
    }
}
